package com.petalsgroup.petals.dao.discuss.impl;

import com.petalsgroup.petals.dao.discuss.DiscussDao;
import com.petalsgroup.petals.pojo.DiscussDO;
import com.petalsgroup.petals.util.JDBCUtils;
import com.petalsgroup.petals.util.RecentlyUtil;
import org.junit.Test;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.object.BatchSqlUpdate;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 操作Discuss表的实现类
 * @author zqm
 */
public class DiscussDaoImpl implements DiscussDao {
    private JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtils.getDataSource());

    /**
     * 添加讨论
     * @author zqm
     * @param discuss
     * @return
     */
    @Override
    public int insertDiscuss(DiscussDO discuss) {
        String sql = "insert into discuss (context, aid, uid, pre_id, create_time) VALUE (?,?,?,?,?)";
        return jdbcTemplate.update(sql, discuss.getContext(), discuss.getAid(), discuss.getUid(), discuss.getPre_id(),
                RecentlyUtil.getStr(discuss.getCreate_time()));
    }

    /**
     * 查询某动漫下的根讨论
     * @param aid
     * @return
     */
    @Override
    public List<DiscussDO> listDiscussByAid(int aid) {
        String sql = "select * from discuss where aid = ? and pre_id = 0 order by create_time desc";
        List<DiscussDO> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(DiscussDO.class), aid);
        return list;
    }

    /**
     * 查询回复当前讨论的讨论
     * @author zqm
     * @param preId
     * @return
     */
    @Override
    public List<DiscussDO> listDiscussByPreId(int preId) {
        String sql = "select * from discuss where pre_id = ? order by create_time desc";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(DiscussDO.class), preId);
    }

    /**
     * 查询待删除评论的id
     * @author zqm
     * @param did
     * @return
     */
    @Override
    public List<Integer> listDeleteDiscuss(int did) {
        String sql = "select did from discuss where pre_id = ?";
        return jdbcTemplate.queryForList(sql, Integer.class, did);
    }

    @Override
    public int updateDiscuss(DiscussDO discuss) {
        String sql = "update discuss set context = ? where did = ?";
        return jdbcTemplate.update(sql, discuss.getContext(), discuss.getDid());
    }

    /**
     * 删除某一条讨论
     * @author zqm
     * @param did
     * @return
     */
    @Override
    public int deleteDiscuss(int did) {
        String sql = "delete from discuss where did = ?";
        return jdbcTemplate.update(sql, did);
    }

    /**
     *
     * @param dis_id
     * @return
     * @author wgd
     */
    @Override
    public Integer findUidByDid(int dis_id) {
        String sql="select uid from discuss where did = ?";
        Integer id = jdbcTemplate.queryForObject(sql, Integer.class, dis_id);
        return id;
    }
}
